package com.hh.controller;

import com.hh.dto.ResultDTO;
import com.hh.service.UserService;
import com.hh.until.Tool;
import com.hh.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/back/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findAll")
    public ResultDTO findAll(UserVO userVO) {
        userVO.setUsername(Tool.nullToStr(userVO.getUsername()));
        userVO.setEnable(Tool.nullToInt(userVO.getEnable()));
        userVO.setSex(Tool.nullToStr(userVO.getSex()));
        userVO.setEmail(Tool.nullToStr(userVO.getEmail()));
        userVO.setStart(Tool.nullToDateTime(userVO.getStart()));
        userVO.setEnd(Tool.nullToDateTime(userVO.getEnd()));
        userVO.setPage(Tool.strToPage(userVO.getPage().toString()));
        userVO.setLimit(Tool.strToLimit(userVO.getLimit().toString()));
        ResultDTO resultDTO = userService.findAll(userVO);
        return resultDTO;
    }

    @GetMapping("/findById")
    public ResultDTO findById(UserVO userVO) {
        userVO.setId(Tool.strToInt(userVO.getId().toString()));

        ResultDTO resultDTO = userService.findById(userVO);
        return resultDTO;
    }

    @PostMapping("/add")
    public ResultDTO add(UserVO userVO) {
        userVO.setUsername(Tool.nullToStr(userVO.getUsername()));
        userVO.setPassword(Tool.nullToStr(userVO.getPassword()));
        userVO.setBirthday(Tool.nullToDate(userVO.getBirthday()));
        userVO.setPhone(Tool.nullToStr(userVO.getPhone()));
        userVO.setEmail(Tool.nullToStr(userVO.getEmail()));
        userVO.setAddress(Tool.nullToStr(userVO.getAddress()));
        userVO.setEnable(Tool.nullToInt(userVO.getEnable()));
        userVO.setSex(Tool.nullToStr(userVO.getSex()));

        ResultDTO resultDTO = userService.add(userVO);
        return resultDTO;
    }

    @PostMapping("/update")
    public ResultDTO update(UserVO userVO) {
        userVO.setId(Tool.strToInt(userVO.getId().toString()));
        userVO.setUsername(Tool.nullToStr(userVO.getUsername()));
        userVO.setBirthday(Tool.nullToDate(userVO.getBirthday()));
        userVO.setPhone(Tool.nullToStr(userVO.getPhone()));
        userVO.setEmail(Tool.nullToStr(userVO.getEmail()));
        userVO.setAddress(Tool.nullToStr(userVO.getAddress()));
        userVO.setEnable(Tool.nullToInt(userVO.getEnable()));
        userVO.setSex(Tool.nullToStr(userVO.getSex()));

        ResultDTO resultDTO = userService.update(userVO);
        return resultDTO;
    }

    @PostMapping("/updatePassword")
    public ResultDTO updatePassword(UserVO userVO) {
        userVO.setId(Tool.strToInt(userVO.getId().toString()));
        userVO.setOldPassword(Tool.nullToStr(userVO.getOldPassword()));
        userVO.setNewPassword(Tool.nullToStr(userVO.getNewPassword()));

        ResultDTO resultDTO = userService.updatePassword(userVO);
        return resultDTO;
    }

    @PostMapping("/delete")
    public ResultDTO delete(UserVO userVO) {
        userVO.setId(Tool.strToInt(userVO.getId().toString()));

        ResultDTO resultDTO = userService.delete(userVO);
        return resultDTO;
    }
}
